package com.czw.web.register

import android.app.Application
import com.didi.drouter.annotation.Service
import com.mtzqc.common.init.InitService
import com.mtzqc.common.manager.LogManager
import com.mtzqc.common.task.ITask
import com.tencent.smtt.export.external.TbsCoreSettings
import com.tencent.smtt.sdk.QbSdk
import com.tencent.smtt.sdk.QbSdk.PreInitCallback


@Service(
    function = [InitService::class]
)
class TbsRegister : InitService {

    private fun initSettings() {
        //TBS内核首次使用时加载卡顿ANR
        QbSdk.initTbsSettings(
            mutableMapOf<String, Any>(
                TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER to true,
                TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE to true
            )
        )
    }

    private fun preInitX5(app: Application) {
        QbSdk.initX5Environment(app, object : PreInitCallback {
            override fun onViewInitFinished(success: Boolean) {
                //x5內核初始化完成的回调，为true表示x5内核加载成功，否则表示x5内核加载失败，会自动切换到系统内核。
                LogManager.d("x5 init is $success")
            }

            override fun onCoreInitFinished() {}
        })
    }

    private fun preInitX5WebCore(app: Application) {
        if (!QbSdk.isTbsCoreInited()) {
            // 设置X5初始化完成的回调接口
            QbSdk.preInit(app, null)
        }
    }

    override val priority: Int
        get() = 0

    override val depName: String
        get() = "TbsX5"
    override val dependsLib: Array<Class<out ITask>>?
        get() = null
    override val depends: Array<String>?
        get() = null

    override fun mainThread(): Boolean {
        return false
    }

    override fun executeInit(app: Application, debug: Boolean, dev: Boolean): Boolean {
        initSettings()
        preInitX5(app)
        preInitX5WebCore(app)
        return true
    }

}